﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;

namespace FAP_GUI.UC
{
    public partial class ucTaskManager : UserControl
    {
        public ucTaskManager()
        {
            InitializeComponent();
        }
        public Thread tCPU;
        private PerformanceCounter theCPUCounter = new PerformanceCounter("Process", "% Processor Time",  Process.GetCurrentProcess().ProcessName);
        //private PerformanceCounter theMomCounter = new PerformanceCounter("Process", "% Processor Time", Process.GetCurrentProcess().ProcessName);
        //int cpuUsage;
        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void ucTaskManager_Load(object sender, EventArgs e)
        {
           // cpuUsage = (int) theCPUCounter.NextValue();
            string processname = Process.GetCurrentProcess().ProcessName;
            if (processname == "devenv")
                return;
            tCPU = new Thread(new ThreadStart(upDatelbCPU));
            tCPU.Start();

        }

        private void upDatelbCPU()
        {
            Process process = Process.GetProcessById(Process.GetCurrentProcess().Id);
            var counter = new PerformanceCounter("Process", "Working Set - Private", Process.GetCurrentProcess().ProcessName);
            Int32 processCPU;
            ProcessThreadCollection myThreads = Process.GetCurrentProcess().Threads;

            //String networkCard = "Qualcomm Atheros AR9002WB-1NG Wireless Network Adapter";
            //PerformanceCounter dataSentCounter =
            //    new PerformanceCounter("Network Interface", "Bytes Sent/sec", networkCard);
            //PerformanceCounter dataReceivedCounter =
            //    new PerformanceCounter("Network Interface", "Bytes Received/sec",networkCard);
            for (int i = 0; i>-1; i++)
            {
                Thread.Sleep(1000);
                processCPU = Convert.ToInt32(theCPUCounter.NextValue());
                lbCPU.Invoke((MethodInvoker)delegate { lbCPU.Text = (processCPU / Environment.ProcessorCount).ToString();/*((int)theCPUCounter.NextValue()).ToString();*/ });
                lbCPU.Invoke((MethodInvoker)delegate { lbRAM.Text = (counter.RawValue / 1024).ToString() + " K"; });
                lbCPU.Invoke((MethodInvoker)delegate { lbNumberThread.Text = myThreads.Count.ToString(); });
                
            }
        }
    }
}
